//
//  ZHToast.m
//  kunlun
//
//  Created by zhusanbao on 2020/10/1.
//  Copyright © 2020 zhusanbao. All rights reserved.
//

#import "ZHToast.h"
#import "KLToast.h"
#import "KLToastConfig.h"

@implementation ZHToast

void toast(id msg){
    if ([msg isKindOfClass:NSString.class]) {
        if ([msg length] > 0) [KLToast showMessage:msg duration:3 finishHandler:nil];
    }else if ([msg respondsToSelector:@selector(description)]){
        [KLToast showMessage:[msg description] duration:2 finishHandler:nil];
    }else{
        NSLog(@"toast 失败");
    }
}
void toastInView(id msg, id view){
    kToastConfig.sv = view;
    toast(msg);
    kToastConfig.sv = nil;
}
void toastSuccess(id msg){
    if ([msg isKindOfClass:NSString.class]) {
        if ([msg length] > 0) [KLToast showSuccessWithMessage:msg duration:3 finishHandler:nil];
    }else if ([msg respondsToSelector:@selector(description)]){
        [KLToast showSuccessWithMessage:[msg description] duration:3 finishHandler:nil];
    }else{
        NSLog(@"toast 失败");
    }
}
void toastSuccessInView(id msg, id view){
    kToastConfig.sv = view;
    toastSuccess(msg);
    kToastConfig.sv = nil;
}
void toastError(id msg){
    if ([msg isKindOfClass:NSString.class]) {
        if ([msg length] > 0) [KLToast showErrorWithMessage:msg duration:3 finishHandler:nil];
    }else if ([msg respondsToSelector:@selector(description)]){
        [KLToast showErrorWithMessage:[msg description] duration:3 finishHandler:nil];
    }else{
        NSLog(@"toast 失败");
    }
}
void toastErrorInView(id msg, id view){
    kToastConfig.sv = view;
    toastError(msg);
    kToastConfig.sv = nil;
}
void toastFormat(id format, ...){
    if (!format) {return;}
    NSString *str = nil;
    if ([format isKindOfClass:[NSString class]]) {
        va_list ap;
        va_start(ap, format);
        str = [[NSString alloc] initWithFormat:format arguments:ap];
        va_end(ap);
    }else{
        str = [format respondsToSelector:@selector(description)] ? [format description] : @"不支持的类型";
    }
    toast(str);
}

void showDebugTips(id message){
#ifdef DEBUG
    toast(message);
#endif
}

void showDebugTipsFormat(id format, ...){
#ifdef DEBUG
    if (!format) {return;}
    NSString *str = nil;
    if ([format isKindOfClass:[NSString class]]) {
        va_list ap;
        va_start(ap, format);
        str = [[NSString alloc] initWithFormat:format arguments:ap];
        va_end(ap);
    }else{
        str = [format respondsToSelector:@selector(description)] ? [format description] : @"不支持的类型";
    }
    toast(str);
#endif
}

@end
